#ifndef CRYPTER_H
#define CRYPTER_H
#include "tomcrypt.h"
#include <QTime>
typedef enum{AES=0,RSA=1,Base64=2} TCipherType;
const int BIT256 = 32;
const int BIT512 = 64;
const int BIT384 = 48;
const int BIT128 = 16;
class DataBlock
{
    unsigned long data_len;
    unsigned char* data;
public:
    DataBlock();
    ~DataBlock();
    DataBlock(unsigned long size);
    void Create(unsigned long size);
    void CreateFrom(const unsigned char* data,const unsigned long size);
    void Add(const unsigned char* data,const unsigned long size);
    void Free();
    unsigned char* Get(unsigned long* size=0);
    unsigned long GetLength();
};

class Crypter
{


    static int hash_idx, prng_idx,cipher_idx;
    static bool isInit;

    bool isRSAInit,isAESInit;
    unsigned char* mDataBuffer;

    prng_state mPrngState;
    gcm_state gcm;

    rsa_key mCipherKey,mSignKey;
    unsigned char mAESKey[16];
    unsigned char mAES_IV[16];
    unsigned char mAES_AUTH[16];

    bool Secure(int error);
    bool Secure();
    public:
        static const unsigned long MAX_BUFFER_SIZE=1024*32;
        Crypter(int size=MAX_BUFFER_SIZE);
        ~Crypter();
        void encode(TCipherType type,  DataBlock& source, DataBlock& dest);
        void decode(TCipherType type,  DataBlock& source, DataBlock& dest);

        void calcSign(DataBlock& data, DataBlock& sign);
        void verifySign(DataBlock& sample, DataBlock& sign,int &correct);


        void GenRSAKeys(DataBlock& public_cipher,DataBlock& public_sign,DataBlock& private_cipher,DataBlock& private_sign);
        void AcceptRSAKeys(DataBlock& cipher,DataBlock& sign,bool full=true);
        void
        ResetRSA();
        void GenAESKeys(DataBlock& pass,DataBlock& iv,DataBlock& auth);
        void AcceptAESKeys(DataBlock& pass,DataBlock& iv,DataBlock& auth);
        bool IsRSAInit(){return isRSAInit;}


};

#endif // CRYPTER_H
